<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">


<ui:composition template="/common/template.xhtml">

	<ui:define name="headSection">

	</ui:define>

	<ui:define name="pageContent">

		<h:form>
			<h:panelGroup>
				<table class="mainTable">
					<tr>
						<td><h:outputLabel value="Име">
								<br />
								<h:inputText value="#{aktualiziraneNaKlient.searchNamePerson}"
									maxlength="50" />
							</h:outputLabel></td>
						<td><h:outputLabel value="Фамилия">
								<br />
								<h:inputText value="#{aktualiziraneNaKlient.searchFamilyPerson}"
									maxlength="50" />
							</h:outputLabel></td>
						<td><h:outputLabel value="Наименование на юридическо лице">
								<br />
								<h:inputText value="#{aktualiziraneNaKlient.searchNameCompany}"
									maxlength="100" />
							</h:outputLabel></td>
					</tr>
					<tr>
						<td><h:commandButton value="Търси" class="regButton"
								style="width:80px;height:40px;margin-left:20px;"
								action="#{aktualiziraneNaKlient.searchIt()}" /> <h:commandButton
								value="Изчисти" class="regButton"
								style="width:80px;height:40px;"
								action="#{aktualiziraneNaKlient.resetSearch()}" /></td>
					</tr>
				</table>
			</h:panelGroup>
		</h:form>

		<div id="scroolTable">
			<!-- scroll bar -->
			<h:form>

				<!-- за Калин - избрания ред ще има клас = "selectedRow", неизбраните = "notSelectedRow" -->
				<h:dataTable rows="#{configurationProperties.getPageSize()}"
					value="#{aktualiziraneNaKlient.spisukKlienti}" first="0" var="curr"
					rowClasses="#{aktualiziraneNaKlient.rowStyleClasses}">


					<h:column>
						<f:facet name="header">Име</f:facet>
						<h:outputText value="#{curr.person.name}"
							rendered="#{not empty curr.person}" />
					</h:column>

					<h:column>
						<f:facet name="header">Фамилия</f:facet>
						<h:outputText value="#{curr.person.family}"
							rendered="#{not empty curr.person}" />
					</h:column>

					<h:column>
						<f:facet name="header">Наименование на юридическо лице</f:facet>
						<h:outputText value="#{curr.company.name}"
							rendered="#{not empty curr.company.name}" />
					</h:column>

					<h:column>
						<f:facet name="header">Населено място</f:facet>
						<h:outputText value="#{curr.addressCity}" />
					</h:column>

					<h:column>
						<f:facet name="header">Адрес</f:facet>
						<h:outputText value="#{curr.addressLine}" />
					</h:column>

					<h:column>
						<f:facet name="header">Телефонен номер</f:facet>
						<h:outputText value="#{curr.phoneNumber}" />
					</h:column>

					<h:column>
						<f:facet name="header">Електронна поща</f:facet>
						<h:outputText value="#{curr.mail}" />
					</h:column>

					<h:column>
						<f:facet name="header">Промени</f:facet>
						<h:commandButton action="#{aktualiziraneNaKlient.selectRow(curr)}"
							value="Промени" class="inTableButton"/>
					</h:column>

					<h:column rendered="#{aktualiziraneNaKlient.choosingAllowed}">
						<f:facet name="header">Избери</f:facet>
						<h:commandButton
							action="#{aktualiziraneNaKlient.chooseClient(curr)}"
							value="Избери" class="inTableButton"/>
					</h:column>

					<f:facet name="footer">

						<!-- предишна страница -->
						<h:commandLink disabled="#{aktualiziraneNaKlient.page - 1 lt 0}"
							action="#{aktualiziraneNaKlient.setPage(aktualiziraneNaKlient.page - 1)}"
							value="&lt;&lt;" />

						<h:outputText value=" " />

						<!-- други страници -->
						<ui:repeat value="#{aktualiziraneNaKlient.pagesList}"
							var="currPage">
							<h:commandLink
								disabled="#{aktualiziraneNaKlient.getPage() eq currPage.intValue() - 1}"
								action="#{aktualiziraneNaKlient.setPage(currPage.intValue() - 1)}"
								value="#{currPage}" />

							<h:outputText value=" " />
						</ui:repeat>

						<!-- следваща страница -->
						<h:commandLink
							disabled="#{aktualiziraneNaKlient.page + 1 ge aktualiziraneNaKlient.pagesCount}"
							action="#{aktualiziraneNaKlient.setPage(aktualiziraneNaKlient.page + 1)}"
							value="&gt;&gt;" />

					</f:facet>

				</h:dataTable>

			</h:form>
		</div>

		<h:form>

			<h:panelGroup rendered="#{aktualiziraneNaKlient.person}">
				<!-- ако клиентът е физическо лице се изчертава това -->
				<table class="mainTable">
					<tr>
						<td><h:outputLabel value="Име*">
								<br />
								<h:inputText id="personname"
									value="#{aktualiziraneNaKlient.namePerson}" maxlength="50"
									required="true" requiredMessage="задължително поле!" 
									validatorMessage="Въведеното име съдържа некоректни символи или е прекалено късо.">
									<f:validateRegex pattern="^[a-zA-Zа-яА-Я]{2,50}$" />
								</h:inputText>
								<br/>
								<h:message for="personname" class="errorMsg" />
							</h:outputLabel></td>
						<td><h:outputLabel value="Фамилия*">
								<br />
								<h:inputText id="family"
									value="#{aktualiziraneNaKlient.familyPerson}" maxlength="50"
									required="true" requiredMessage="задължително поле!" 
									validatorMessage="Въведената фамилия съдържа некоректни символи или е прекалено къса.">
									<f:validateRegex pattern="^[a-zA-Zа-яА-Я]{2,50}$" />
								</h:inputText>
								<br/>
								<h:message for="family" class="errorMsg" />
							</h:outputLabel></td>
					</tr>
				</table>
			</h:panelGroup>

			<h:panelGroup rendered="#{aktualiziraneNaKlient.company}">
				<!-- ако клиентът е физическо лице се изчертава това -->
				<table class="mainTable">
					<tr>
						<td><h:outputLabel value="Наименование на компанията*">
								<br />
								<h:inputText id="companyname"
									value="#{aktualiziraneNaKlient.nameCompany}" maxlength="100"
									required="true" requiredMessage="задължително поле!" 
									validatorMessage="Въведеното наименование е прекалено късо или съдържа некоректни символи.">
									<f:validateRegex pattern="^[а-яА-Я\s\w]{3,100}$" />
								</h:inputText>
								<br/>
								<h:message for="companyname" class="errorMsg" />
							</h:outputLabel></td>
						<td><h:outputLabel value="Единен идентификационен код (ЕИК)*">
								<br />
								<h:inputText id="regnumber"
									value="#{aktualiziraneNaKlient.registrationNumberCompany}"
									maxlength="9" required="#{aktualiziraneNaKlient.company}"
									requiredMessage="задължително поле!" 
									validatorMessage="ЕИК трябва да се състои от точно 9 цифри.">
									<f:validateRegex pattern="^[0-9]{9}$" />
								</h:inputText>
								<br/>
								<h:message for="regnumber" class="errorMsg" />
							</h:outputLabel></td>
					</tr>
					<tr>
						<td><h:outputLabel value="ДДС номер">
								<br />
								<h:inputText id="vat"
									value="#{aktualiziraneNaKlient.VATNumberCompany}"
									maxlength="11" 
									validatorMessage="ДДС номерът се състои от 11 символа - главни латински букви и цифри.">
									<f:validateRegex pattern="^[A-Z0-9]{11}$" />
								</h:inputText>
								<br/>
								<h:message for="vat" class="errorMsg" />
							</h:outputLabel></td>
						<td><h:outputLabel value="Лице за контакт">
								<br />
								<h:inputText id="contactperson"
									value="#{aktualiziraneNaKlient.contactPersonCompany}"
									maxlength="100" 
									validatorMessage="Въведеното име съдържа некоректни символи или е прекалено късо.">
									<f:validateRegex pattern="^[a-zA-Zа-яА-Я\s]{2,100}$" />
								</h:inputText>
								<br/>
								<h:message for="contactperson" class="errorMsg" />
							</h:outputLabel></td>
					</tr>
				</table>
			</h:panelGroup>
			<table class="mainTable">
				<tr>
					<td><h:outputLabel value="Населено място*">
							<br />
							<h:inputText id="city"
								value="#{aktualiziraneNaKlient.addressCity}" maxlength="30"
								required="true" requiredMessage="задължително поле!"
								readonly="#{!aktualiziraneNaKlient.rowSelected}" 
								validatorMessage="Въведеното населено място съдържа некоректни символи или е прекалено късо.">
								<f:validateRegex pattern="^[a-zA-Zа-яА-Я]{3,30}$" />
							</h:inputText>
							<br/>
							<h:message for="city" class="errorMsg" />
						</h:outputLabel></td>
					<td><h:outputLabel value="Адрес*">
							<br />
							<h:inputText id="address"
								value="#{aktualiziraneNaKlient.addressLine}" maxlength="100"
								required="true" requiredMessage="задължително поле!"
								readonly="#{!aktualiziraneNaKlient.rowSelected}" 
								validatorMessage="Въведеният адрес е прекалено къс.">
								<f:validateRegex pattern="^.{5,100}$" />
							</h:inputText>
							<br/>
							<h:message for="address" class="errorMsg" />
						</h:outputLabel></td>
					<td><h:outputLabel value="Телефонен номер">
							<br />
							<h:inputText id="phone"
								value="#{aktualiziraneNaKlient.phoneNumber}" maxlength="15"
								readonly="#{!aktualiziraneNaKlient.rowSelected}"
								validatorMessage="Телефонният номер е невалиден. Примери: +359885609456, 082860450">
								<f:validateRegex pattern="^[+]?[0-9]{6,14}$" />
							</h:inputText>
							<br/>
							<h:message for="phone" class="errorMsg" />
						</h:outputLabel></td>
				</tr>
				<tr>
					<td><h:outputLabel value="Електронна поща">
							<br />
							<h:inputText id="mail" value="#{aktualiziraneNaKlient.mail}"
								maxlength="50" readonly="#{!aktualiziraneNaKlient.rowSelected}"
								validatorMessage="Електронната поща е невалидна. Пример: potrbitel@mail.bg">
								<f:validateRegex pattern="^[a-zA-Z][a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$" />
							</h:inputText>
							<br/>
							<h:message for="mail" class="errorMsg" />
						</h:outputLabel></td>
					<td><h:outputLabel value="Номер на банковата сметка">
							<br />
							<h:inputText id="iban"
								value="#{aktualiziraneNaKlient.IBANNumber}" maxlength="30"
								readonly="#{!aktualiziraneNaKlient.rowSelected}" 
								validatorMessage="Позволени символи за номер на сметка са главни латински букви и цифри. От 10 до 30 символа">
								<f:validateRegex pattern="^[A-Z0-9]{10,30}$" />
							</h:inputText>
							<br/>
							<h:message for="iban" class="errorMsg" />
						</h:outputLabel></td>
					<td><h:outputLabel value="Банков код">
							<br />
							<h:inputText id="swiftcode"
								value="#{aktualiziraneNaKlient.SWIFTCode}" maxlength="20"
								readonly="#{!aktualiziraneNaKlient.rowSelected}" 
								validatorMessage="Позволени символи за банков код са главни латински букви и цифри. От 8 до 11 символа.">
								<f:validateRegex pattern="^[A-Z0-9]{8,11}$" />
							</h:inputText>
							<br/>
							<h:message for="swiftcode" class="errorMsg" />
						</h:outputLabel></td>
				</tr>
				<tr>
					<td><h:commandButton value="Запази" class="regButton"
							style="width:80px;height:40px;margin-left:20px;"
							action="#{aktualiziraneNaKlient.saveClient()}"
							disabled="#{!aktualiziraneNaKlient.rowSelected or !aktualiziraneNaKlient.changingAllowed}" />
					</td>
				</tr>
			</table>
		</h:form>

		<h:form>
			<!-- формата е разделена на две, за да не се проверяват данните
			при натискането на бутоните "Отмени" и "Добави" -->
			<table class="buttonTable" style="margin-left: 35px;">
				<tr>
					<td><h:commandButton
							action="#{aktualiziraneNaKlient.deselectRow()}" value="Отмени"
							class="regButton" style="width:80px;height:40px;"
							disabled="#{!aktualiziraneNaKlient.rowSelected}" /> <h:commandButton
							action="#{aktualiziraneNaKlient.goToAdd()}" value="Добави"
							class="regButton" style="width:80px;height:40px;"
							disabled="#{!aktualiziraneNaKlient.goToAddAllowed}" /> <h:outputText
							value="#{aktualiziraneNaKlient.errorMessage}" /></td>
				</tr>
			</table>
		</h:form>
	</ui:define>
</ui:composition>
</html>